<?php
include ORG_PATH . "/Smarty/Smarty.class.php";

class SmartyView{
	private static $smarty = NULL;	
	public function __construct(){
		if(!is_null(self::$smarty)) return;		
		//实例化smarty
		$smarty = new Smarty();
		//定义smarty模板目录
		$smarty->template_dir = APP_VIEW_PATH . '/' . CONTROLLER;
		//定义解析目录
		$smarty->compile_dir = APP_COMPILE_PATH;
		//定义缓存目录
		$smarty->cache_dir = APP_CACHE_PATH;
		//缓存开启否
		$smarty->caching = C('CACHE_ON');
		//缓存时间
		 $smarty->cache_lifetime = C('CACHE_TIME');
		//开始定界符
		$smarty->left_delimiter = C('LEFT_DELIMITER');
		//结束定界符
		$smarty->right_delimiter = C('RIGHT_DELIMITER');
		//注册无需缓存区块
		$smarty->register_block("nocache","nocache",false);
		//保存smarty对象到静态属性中
		self::$smarty = $smarty;
	}	
	/*
	 * 分配变量
	 */
	protected function assign($var,$value){
		self::$smarty->assign($var,$value);
	}
	/*
	 *显示模板 
	 */
	public function display(){
		//模板路径
//		$path = APP_VIEW_PATH . '/' . CONTROLLER . '/' . ACTION . '.HTML';
		$path = APP_VIEW_PATH . '/' . APP_NAME . '/' . CONTROLLER .'_'. ACTION . '.html';
		//判断路径 载入路径
		if(is_file($path)){
			self::$smarty->display($path);
		}else{
			halt($path . '模板未找到！');
		}		
	}
	/*
	 * 判断缓存时效性
	 */
	protected function is_cached(){
		//组合路径
//		$path = APP_VIEW_PATH . '/' .CONTROLLER. '/' .ACTION . '.html';
		$path = APP_VIEW_PATH . '/' . APP_NAME . '/' . CONTROLLER .'_'. ACTION . '.html';
		if(is_file($path)){
			return self::$marty->is_cached($path);
		}else{
			halt($path . '模板不存在！');
		}
	}
}



